<%@page import="Entity.Manager"%>
<%@page import="DAO.DAO_Manager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login</title>
        <link rel="stylesheet" href="Assets/css/bootstrap.min.css" />
        <link rel="stylesheet" href="Assets/css/font-awesome.min.css" />
        <link rel="stylesheet" href="Assets/css/animate.css" />
        <link rel="stylesheet" href="Assets/css/style.css" />
        <script src="Assets/js/bootstrap.min.js"></script>
        <script src="Assets/js/jquery-2.1.1.min.js"></script>    
        <style>
            body {
                background: #777;
            }
            .alert {
                margin: 5px 0 0;
            }
        </style>
    </head>
    <body>            
        <div id="LoginForm">
            <form method="POST">
                <input type="text" name="username" class="form-control" placeholder="Username">
                <input type="password" name="password" class="form-control" placeholder="Password">
                <input type="submit" name="submit" value="Login" class="btn btn-primary btn-block">
            </form>
            <%
                if ((String) request.getParameter("submit") != null) {
                    String username = request.getParameter("username");
                    String password = request.getParameter("password");
                    out.print("<div class=\"alert alert-danger\" role=\"alert\">");
                    if (username == null || username.isEmpty()) {
                        out.print("<p class='animated shake'>Username cannot be blank!</p>");
                    }
                    if (password == null || password.isEmpty()) {
                        out.print("<p class='animated shake'>Password cannot be blank!</p>");
                    }
                    if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
                        DAO_Manager dao = new DAO_Manager();
                        if (dao.checkLogin(new Manager(username, password))) {
                            //create new session
                            session.setAttribute("loggedUser", username);
                            response.sendRedirect("Student.jsp");
                        } else {
                            out.print("<p class='animated shake'>Username or password is incorrect!</p>");
                        }
                    }
                    out.print("</div>");
                }
            %>            
        </div>

    </body>
</html>
